package cn.machen.www.fashion.services;

import cn.machen.www.fashion.dao.UserDao;
import cn.machen.www.fashion.pojo.User;
import cn.machen.www.fashion.utils.Exception.UserException;

public class UserService {
    private UserDao userDao = new UserDao();

    public void active(String code) throws UserException{
        User user = userDao.findByCode(code);
        if(user == null ) throw new UserException("激活失败");
        if(user.getState() == 1) throw new UserException("邮箱只能激活一次");
        userDao.updataStatus(1, user.getUid());
    }

    public void regesit(User user) throws UserException {
        //检验用户名
        User result = userDao.findByUsername(user.getUsername());
        if(result!=null)
            throw new UserException("用户已注册");
        //检验邮箱
         result = userDao.findByUserEmail(user.getEmail());
         if(result!=null)
            throw new UserException("用户邮箱已经注册");
        //添加用户
        userDao.add(user);
    }


    public User login(User user) throws UserException {
        if(user.getUsername() == null || ("").equalsIgnoreCase(user.getUsername().trim()))
                throw new UserException("用户名为空！");
        User result = userDao.findByUsername(user.getUsername());
        if(!result.getPassword().equals(user.getPassword()))
            throw new UserException("用户名或密码错误");
        if(result.getState()==0)
            throw new UserException("用户邮箱未激活，请前往激活");
        return result;
    }
}
